import java.util.Stack;

public class Solution907 {
    public int sumSubarrayMins(int[] arr) {
        Stack<Integer> stack=new Stack<>();
        int m=1000000007;
        int[] f=new int[arr.length];
        for (int i=0;i<arr.length;i++){
            while (!stack.isEmpty() && arr[i]<arr[stack.peek()]){
                stack.pop();
            }
            if (stack.isEmpty()){
                f[i]=i;
            }else{
                f[i]=i-stack.peek()-1;
            }
            stack.push(i);
        }
        int[] g=new int[arr.length];
        stack.clear();
        for (int i=arr.length-1;i>=0;i--){
            while (!stack.isEmpty() && arr[i]<=arr[stack.peek()]){
                stack.pop();
            }
            if (stack.isEmpty()){
                g[i]=arr.length-i-1;
            }else{
                g[i]=stack.peek()-i-1;
            }
            stack.push(i);
        }
        long ans=0;
        for (int i=0;i<arr.length;i++){
            ans=(ans+((long)(f[i]+1)*(g[i]+1)%m*arr[i]%m)%m)%m;
        }
        return (int)ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution907().sumSubarrayMins(new int[]{11,81,94,43,3}));
    }
}
